from PySide6.QtWidgets import QToolBar
import subprocess


class R:
    """常量和帮助方法"""

    AppName = "Media Player"
    AppVersion = "0.0.1"

    @staticmethod
    def AddActions(target, actions):
        for action in actions:
            if action is None:
                if not isinstance(target, QToolBar):
                    target.addSeparator()
            elif isinstance(target, QToolBar) and action.icon is None:
                continue
            else:
                target.addAction(action)

    import subprocess

    @staticmethod
    def FfmpegDownload(m3u8_url, output_path="output.mp4"):
        """
        使用 ffmpeg 下载 m3u8（需要系统安装 ffmpeg）
        """
        try:
            # 检查 ffmpeg 是否可用
            subprocess.run(["ffmpeg", "-version"], capture_output=True, check=True)

            # 使用 ffmpeg 下载
            cmd = [
                "ffmpeg",
                "-i",
                m3u8_url,
                "-c",
                "copy",
                "-bsf:a",
                "aac_adtstoasc",
                output_path,
                "-y",  # 覆盖已存在文件
            ]

            print("开始下载...")
            result = subprocess.run(cmd, capture_output=True, text=True)

            if result.returncode == 0:
                print(f"下载完成: {output_path}")
                return True
            else:
                print(f"ffmpeg 错误: {result.stderr}")
                return False

        except subprocess.CalledProcessError:
            print("请确保已安装 ffmpeg")
            return False
        except Exception as e:
            print(f"下载失败: {e}")
            return False
